package cn.csust.net2.manager.client.panel;

import java.util.Date;

import cn.csust.net2.manager.client.app.App;
import cn.csust.net2.manager.client.ux.BeanGridPanel;
import cn.csust.net2.manager.shared.po.Teacher;
import cn.csust.net2.manager.shared.po.Work;
import cn.csust.net2.manager.shared.service.TeacherInformationServiceAsync;
import cn.csust.net2.manager.shared.util.Factory;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelFactory;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class WorkSituationPanel extends ContentPanel {
	
	private BeanGridPanel<Work> workGridPanel;
	private TextField<String> workUnitTextField;
	private TextField<String> workPositionTextField;
	private DateField startDateDateField;
	private TextField<String> gradeTextField;
	private TextField<String> workDetailsTextField;
	
	private Teacher teacher;
	protected TeacherInformationServiceAsync service;
	
	public WorkSituationPanel(TeacherInformationServiceAsync service) {
		setHeaderVisible(false);
		setScrollMode(Scroll.AUTO);
		setLayout(new FitLayout());
		this.service = service;
		teacher = (Teacher) App.getUser();
		create();
	}
	
	public void create() {
		FormData formData = new FormData("100%");  
	  
	    ContentPanel main = new ContentPanel();
	    main.setHeaderVisible(false);
	    main.setFrame(true);
	    main.setLayout(new ColumnLayout());  
	    
	    //左边现任工作
	    final FormPanel formPanelLeft = new FormPanel();  
	    formPanelLeft.setHeaderVisible(false);
	    formPanelLeft.setLayout(new FlowLayout());
	    
	    FieldSet fieldSetLeft = new FieldSet();  
	    fieldSetLeft.setHeading("现任岗位");  
	    fieldSetLeft.setHeight(400); 
	    
	    FormLayout formLayout = new FormLayout();  
	    formLayout.setLabelWidth(85);  
	    fieldSetLeft.setLayout(formLayout);  
	  
	    workUnitTextField = new TextField<String>();  
	    workUnitTextField.setFieldLabel("工作单位");  
	    workUnitTextField.setValue(teacher.getWorkUnit());
	    workUnitTextField.setEnabled(false);
	    fieldSetLeft.add(workUnitTextField, formData);  
	  
	    workPositionTextField = new TextField<String>();  
	    workPositionTextField.setFieldLabel("工作岗位");
	    workPositionTextField.setValue(teacher.getWorkPosition());
	    workPositionTextField.setEnabled(false);
	    fieldSetLeft.add(workPositionTextField, formData);  
	  
	    startDateDateField = Factory.createDateFieldWithFormated();  
	    startDateDateField.setFieldLabel("开始时间"); 
	    if(teacher.getStartDate()!=null) {
	    	startDateDateField.setValue(teacher.getStartDate());
	    }
	    startDateDateField.setEnabled(false);
	    fieldSetLeft.add(startDateDateField, formData);  
	    
	    gradeTextField = new TextField<String>();  
	    gradeTextField.setFieldLabel("负责年级");  
	    gradeTextField.setValue(teacher.getGrade());
	    gradeTextField.setEnabled(false);
	    fieldSetLeft.add(gradeTextField, formData);  
	    
	    workDetailsTextField = new TextField<String>();  
	    workDetailsTextField.setFieldLabel("负责专项工作"); 
	    workDetailsTextField.setValue(teacher.getWorkDeatails());
	    workDetailsTextField.setEnabled(false);
	    fieldSetLeft.add(workDetailsTextField, formData);  
	    
	    
	    ContentPanel cp = new ContentPanel();  
	    cp.setHeaderVisible(false);
	    final Button submit = new Button("提交");
	    submit.setEnabled(false);
	    Button modify = new Button("开始修改");
	    modify.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				for(Field<?> field :formPanelLeft.getFields()) {
					field.setEnabled(true);
				}
				submit.setEnabled(true);
			}
		});
	    cp.addButton(modify);
	    submit.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO Auto-generated method stub
				if(startDateDateField.isValid()) {
					submit();
				} else {
					MessageBox.alert("提示", "对不起，您的输入有误，请重新输入！", null);
				}
				
			}
		});
	    cp.addButton(submit);
	    cp.setButtonAlign(HorizontalAlignment.CENTER);  
	        
	    
	    fieldSetLeft.add(cp);
	  
	    formPanelLeft.add(fieldSetLeft);
	    
	    
	    //右边曾任工作
	    FormPanel formPanelRight = new FormPanel();  
	    formPanelRight.setHeaderVisible(false);
	    formPanelRight.setLayout(new FlowLayout());
	    
	    FieldSet fieldSetRight = new FieldSet();  
	    fieldSetRight.setHeading("曾任岗位");  
	    
	    workGridPanel = new BeanGridPanel<Work>(new Work(),service);
	    workGridPanel.setAutoLoad(true);
	    workGridPanel.createDefault();
	    workGridPanel.setHeight(363);
	    
	    fieldSetRight.add(workGridPanel);
	    formPanelRight.add(fieldSetRight);
	  
	    main.add(formPanelLeft, new ColumnData(.3));  
	    main.add(formPanelRight, new ColumnData(.7));  
	  
	    this.add(main);
	}
	
	public void submit() {
		addUsedWork();
		modifyCurrentPosition();
	}
	
	/**
	 * 将老师当前的工作岗位信息加入到曾任岗位信息表中
	 */
	public void addUsedWork() {
		Work work = new Work();
		work.setUser(teacher);
		work.setWorkUnit(teacher.getWorkUnit());
		work.setWorkPosition(teacher.getWorkPosition());
		if(teacher.getStartDate()!=null) {
			work.setStartDate(teacher.getStartDate());
		}
		work.setEndDate(new Date());
		work.setWorkDetails(teacher.getWorkDeatails());
		BeanModelFactory beanModelFactory = BeanModelLookup.get().getFactory(Work.class);
		BeanModel model = beanModelFactory.createModel(work);
		workGridPanel.getGrid().getStore().insert(model, 0);
		workGridPanel.getGrid().getView().refresh(false);
		service.save(work, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Void result) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	/**
	 * 修改老师当前的工作情况
	 */
	public void modifyCurrentPosition() {
		Teacher teacher = (Teacher)App.getUser();
		teacher.setWorkUnit(workUnitTextField.getValue());
		teacher.setWorkPosition(workPositionTextField.getValue());
		teacher.setStartDate(new Date());
		teacher.setGrade(gradeTextField.getValue());
		teacher.setWorkDeatails(workDetailsTextField.getValue());
		service.update(teacher, new AsyncCallback<Void>() {
			
			@Override
			public void onSuccess(Void result) {
				// TODO Auto-generated method stub
				Info.display("提示", "修改成功");
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Info.display("提示", "修改失败");
			}
		});
	}
}
